package mi.payment.settings;

import mi.payment.utils.SharedPreferencesUtil;

/**
 * 免签免密参数保存管理类
 */
public class QPSParamsOperation {

    private static QPSParamsOperation instance;
    private QPSParamsSetting instanceSettings;

    private QPSParamsOperation() {
        instanceSettings = getQPSParamsSettings();
        if (instanceSettings == null) {
            instanceSettings = new QPSParamsSetting();
            saveQPSParamsSetting(instanceSettings);
        }
    }

    public static QPSParamsOperation getInstance() {
        if (instance == null) {
            instance = new QPSParamsOperation();
        }
        return instance;
    }

    public QPSParamsSetting getQPSParamsSettings() {
        if (instanceSettings == null) {
            Object obj = SharedPreferencesUtil.readObj(QPSParamsSetting.QPS_PARAMS_SETTINGS_KET);
            if (obj instanceof QPSParamsSetting) {
                instanceSettings = (QPSParamsSetting) obj;
            } else {
                instanceSettings = new QPSParamsSetting();
                saveQPSParamsSetting(instanceSettings);
            }
        }
        return instanceSettings;
    }

    public void saveQPSParamsSetting(QPSParamsSetting qpsParamsSetting) {
        this.instanceSettings = qpsParamsSetting;
        SharedPreferencesUtil.saveObj(qpsParamsSetting, QPSParamsSetting.QPS_PARAMS_SETTINGS_KET);
    }

    public void clearTransactionOtherSettings() {
        SharedPreferencesUtil.clearByKey(QPSParamsSetting.QPS_PARAMS_SETTINGS_KET);
    }

    /**
     * QpsParamFF803A
     * 闪卡当笔重刷处理时间 N3 默认值10秒
     */
    public void saveQpsParamFF803A(long qpsParamFF803A) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamFF803A(qpsParamFF803A);
        saveQPSParamsSetting(other);
    }

    /**
     * QpsParamFF803C
     * 闪卡记录可处理时间 N3 默认值60秒
     */
    public void saveQpsParamFF803C(long qpsParamFF803C) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamFF803C(qpsParamFF803C);
        saveQPSParamsSetting(other);
    }

    /**
     * QpsParamNeedNoPswId
     * 非接快速业务标识 N1 终端使用此数据元作为是否开启非接快速功能的判断条件。1-启用;0-关闭
     */
    public void saveQpsParamNeedNoPswId(boolean qpsParamNeedNoPswId) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamNeedNoPswId(qpsParamNeedNoPswId);
        saveQPSParamsSetting(other);
    }

    /**
     * QpsParamBinAId
     * BIN表A标识 N1
     */
    public void saveQpsParamBinAId(boolean qpsParamBinAId) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamBinAId(qpsParamBinAId);
        saveQPSParamsSetting(other);
    }

    /**
     * QpsParamBinBId
     * BIN表B标识 N1
     */
    public void saveQpsParamBinBId(boolean qpsParamBinBId) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamBinBId(qpsParamBinBId);
        saveQPSParamsSetting(other);
    }

    /**
     * QpsParamCDCVMId
     * CD CVM标识 N1 终端使用此数据元作为是否将卡片CDCVM执行情况作为免密的判断条件。1-启用;0-关闭
     */
    public void saveQpsParamCDCVMId(boolean qpsParamCDCVMId) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamCDCVMId(qpsParamCDCVMId);
        saveQPSParamsSetting(other);
    }

    /**
     * QpsParamNeedNoPswLimit
     * 非接快速业务（QPS）免密限额 N12 终端使用此数据元作为条件之一判断非接联机交易是否请求持卡人验证方法，默认值300元，精确至小数点后两位
     */
    public void saveQpsParamNeedNoPswLimit(long qpsParamNeedNoPswLimit) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamNeedNoPswLimit(qpsParamNeedNoPswLimit);
        saveQPSParamsSetting(other);
    }

    /**
     * QpsParamNeedNoSignLimit
     * 免签限额 N12 终端使用此数据元作为判断交易凭证是否需要进行免签处理,默认值为300元，精确至小数点后两位
     */
    public void saveQpsParamNeedNoSignLimit(long qpsParamNeedNoSignLimit) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamNeedNoSignLimit(qpsParamNeedNoSignLimit);
        saveQPSParamsSetting(other);
    }

    /**
     * QpsParamNeedNoSignId
     * 免签标识 N1 终端使用此数据元作为是否支持交易凭证免签处理的判断条件 1， 启用;0，关闭
     */
    public void saveQpsParamNeedNoSignId(boolean qpsParamNeedNoSignId) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamNeedNoSignId(qpsParamNeedNoSignId);
        saveQPSParamsSetting(other);
    }

    /**
     * QpsParamQpsTubeId
     * 非接交易通道开关 N1 0-优先联机借贷记;1-优先电子现金
     */
    public void saveQpsParamQpsTubeId(boolean qpsParamQpsTubeId) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamQpsTubeId(qpsParamQpsTubeId);
        saveQPSParamsSetting(other);
    }

    /**
     * 保存支持双免的卡BIN
     */
    public void saveCardBinB(String cardBinB) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setCardBinB(cardBinB);
        saveQPSParamsSetting(other);
    }

    /**
     * 保存不允许交易的卡BIN（黑名单）
     */
    public void saveCardBinC(String cardBinC) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setCardBinC(cardBinC);
        saveQPSParamsSetting(other);
    }

    /**
     * QpsParamBinCId
     * BIN表C标识(黑名单) N1
     */
    public void saveQpsParamBinCId(boolean qpsParamBinCId) {
        QPSParamsSetting other = getQPSParamsSettings();
        other.setQpsParamBinCId(qpsParamBinCId);
        saveQPSParamsSetting(other);
    }


}
